#include "gp_layout_map.h"
#include <iostream>
#include "json/json.h"

#include <fstream>
#include <string.h>

CGpLayout * CGpLayout::m_pInstance = NULL;
CGpLayout::CGarbo CGpLayout::Garbo;


CGpLayout* CGpLayout::get_instance(void)
{
    if(CGpLayout::m_pInstance==NULL)
    {
        CGpLayout::m_pInstance = new CGpLayout(LAYOUT_FILE);
    }
    return CGpLayout::m_pInstance;
    
}


CGpLayout::CGpLayout(const char * fileIn)
{
    int ret = load_layout(fileIn);

    std::cout<<"json file:"<<fileIn<<std::endl;

    if(ret != 0)
    {
        std::cout<<"load layout failed,ret="<<ret<<std::endl;
    }
}

int get_one_loc_group(const Json::Value & valueIn,multi_module_loc * locationOut)
{
    const int numParam = 10;
    std::vector<std::string> strArrOneLoc(numParam);
    strArrOneLoc.clear();
    strArrOneLoc.push_back("ex_delta_x");
    strArrOneLoc.push_back("ex_delta_y");
    strArrOneLoc.push_back("max_num_x");
    strArrOneLoc.push_back("max_num_y");
    strArrOneLoc.push_back("init_x");
    strArrOneLoc.push_back("init_y");
    strArrOneLoc.push_back("delta_x");
    strArrOneLoc.push_back("delta_y");
    strArrOneLoc.push_back("max_index_x");
    strArrOneLoc.push_back("max_index_y");

    char flag[10];
    
    Json::Value::Members listNameParam = valueIn.getMemberNames();
    Json::Value::Members::iterator itOneParam = listNameParam.begin();


    std::vector<std::string>::iterator itStr = strArrOneLoc.begin();
    int indexStr = 0;
    memset(flag,0,10);

    locationOut->firstModuleLoc.max_num_x = 1;
    locationOut->firstModuleLoc.max_num_y = 1;

    locationOut->firstModuleLoc.delta_x = 0;
    locationOut->firstModuleLoc.delta_y = 0;
    locationOut->delta_module_x = 0;
    locationOut->delta_module_y = 0;
    locationOut->max_num_module_x = 1;
    locationOut->max_num_module_y = 1;

    while(itOneParam!=listNameParam.end())
    {
        indexStr = 0;
        for(indexStr=(strArrOneLoc.size()-1);indexStr>=0;indexStr--)
        {
            if(flag[indexStr])
            {
                continue;
            }
           if((*itOneParam).compare(strArrOneLoc[indexStr])==0)
           {
               switch (indexStr)
               {
               case 0:
               {
                  locationOut->delta_module_x = valueIn[*itOneParam].asDouble();
               }
               break;
               case 1:
               {
                  locationOut->delta_module_y = valueIn[*itOneParam].asDouble();
               }
               break;
               case 2:
               {
                  locationOut->max_num_module_x = valueIn[*itOneParam].asInt();
               }
               break;
               case 3:
               {
                  locationOut->max_num_module_y = valueIn[*itOneParam].asInt();
               }
               break;
               case 4:
               {
                  locationOut->firstModuleLoc.init_x = valueIn[*itOneParam].asInt();
               }
               break;

               case 5:
               {
                  locationOut->firstModuleLoc.init_y = valueIn[*itOneParam].asInt();
               }
               break;

               case 6:
               {
                  locationOut->firstModuleLoc.delta_x = valueIn[*itOneParam].asDouble();
               }
               break;

               case 7:
               {
                  locationOut->firstModuleLoc.delta_y = valueIn[*itOneParam].asDouble();
               }
               break;

               case 8:
               {
                  locationOut->firstModuleLoc.max_num_x = valueIn[*itOneParam].asInt();
               }
               break;

               case 9:
               {
                  locationOut->firstModuleLoc.max_num_y = valueIn[*itOneParam].asInt();
               }
               break;
               
               default:
                   {
                       std::cout<<"get_one_loc_group,"<<*itOneParam<<" is shoot"<<",index="<<indexStr<<std::endl;
                   }
                   break;
               }
//               if(3==indexStr || 7==indexStr)
//               {
//                   std::cout<<"get_one_loc_group,"<<*itOneParam<<" is shoot"<<",index="<<indexStr<<std::endl;
//               }
               flag[indexStr] = 1;
               break;
           } 
        }   
        itOneParam++;
    }
    return 0;
}

int parseDp8Locations(const Json::Value & valueIn,multi_module_loc ** locationOutArr,int * numOut)
{
    const int numStrDp8 = 19;
    std::vector<std::string> strArrDp8(numStrDp8);
    * numOut = valueIn.size();
     
    * locationOutArr = new multi_module_loc[* numOut]; 
    multi_module_loc * ptrResult = * locationOutArr;  
    strArrDp8[0] = "pcr_trans";
    strArrDp8[1] = "pcr_buff_tip";
    strArrDp8[2] = "pcr_buff_jaw";
    strArrDp8[3] = "pcr_buff_tip_1";
    strArrDp8[4] = "dna_buff_jaw";
    strArrDp8[5] = "dna_buff_tip";
	strArrDp8[6] = "dna_buff_tip_1";
    strArrDp8[7] = "tips48_trans";//for 8tips to trans dna
    strArrDp8[8] = "tips48_purify_a";
	strArrDp8[9] = "tips48_purify_b";
    strArrDp8[10] = "tips48_buff";//for 1tip to trans dna

    strArrDp8[11] = "reagent";
    strArrDp8[12] = "tube8_shake";
    strArrDp8[13] = "droptip";
	strArrDp8[14] = "droptip_1";
    strArrDp8[15] = "film_src";
    strArrDp8[16] = "seal";
    strArrDp8[17] = "barcode";    
    strArrDp8[18] = "waste_liquid";

    std::cout<<"size Arr ="<<strArrDp8.size()<<std::endl;
    char * flag = new char[numStrDp8];
    memset(flag,0,numStrDp8);

    Json::Value::Members listNameModule = valueIn.getMemberNames();
    Json::Value::Members::iterator itDp8Loc = listNameModule.begin();
    
    //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
    int indexDp8 = 0;   
    while(itDp8Loc!=listNameModule.end())
    {
        indexDp8 = 0;
        for(indexDp8=(strArrDp8.size()-1);indexDp8>=0;indexDp8--)
        {
            if(flag[indexDp8])
            {
                continue;
            }
            if((*itDp8Loc).compare(strArrDp8[indexDp8])==0)
            {
                switch(indexDp8)
                {
                    case 0:
                    {
                        ptrResult->locationType = DP8_PCR_TRANS;                            
                    }
                    break;
                    case 1:
                    {
                        ptrResult->locationType = DP8_PCR_BUFF_TIP;                            
                    }
                    break;
                    case 2:
                    {
                        ptrResult->locationType = DP8_PCR_BUFF_JAW;                            
                    }
                    break;
					case 3:
                    {
                        ptrResult->locationType = DP8_PCR_BUFF_TIP_1;                            
                    }
                    break;
                    case 4:
                    {
                        ptrResult->locationType = DP8_DNA_BUFF_JAW;                            
                    }
                    break;
                    case 5:
                    {
                        ptrResult->locationType = DP8_DNA_BUFF_TIP;                            
                    }
                    break;
					case 6:
                    {
                        ptrResult->locationType = DP8_DNA_BUFF_TIP_1;                            
                    }
                    break;
                    case 7:
                    {
                        ptrResult->locationType = DP8_TIP48_TRANS;                            
                    }
                    break;
                    case 8:
                    {
                        ptrResult->locationType = DP8_TIP48_PURIFY_A;                            
                    }
                    break;
                    case 9:
                    {
                        ptrResult->locationType = DP8_TIP48_PURIFY_B;                            
                    }
                    break;
                    case 10:
                    {
                        ptrResult->locationType = DP8_TIP48_BUFF;                            
                    }
                    break;
                    case 11:
                    {
                        ptrResult->locationType = DP8_REAGENT;                            
                    }
                    break;
                    case 12:
                    {
                        ptrResult->locationType = DP8_TUBE8_SHAKE;                            
                    }
                    break;

                    case 13:
                    {
                        ptrResult->locationType = DP8_DROPTIP;                            
                    }
                    break;
                    case 14:
                    {
                        ptrResult->locationType = DP8_DROPTIP_1;                            
                    }
                    break;
                    case 15:
                    {
                        ptrResult->locationType = DP8_FILM_SRC;                            
                    }
                    break;
                    case 16:
                    {
                        ptrResult->locationType = DP8_HEAT_SEAL;                            
                    }
                    break;

                    case 17:
                    {
                        ptrResult->locationType = DP8_SCANCODE;                            
                    }
                    break;
                    case 18:
                    {
                        ptrResult->locationType = DP8_WASTE_LIQUID;                            
                    }
                    break;

                    default:
                    {
                        std::cout<<*itDp8Loc<<" is shoot"<<",index="<<indexDp8<<std::endl;
                    }
                    break;
                }
                std::cout<<*itDp8Loc<<",set type to "<<ptrResult->locationType<<",index="<<indexDp8<<std::endl;
                get_one_loc_group(valueIn[*itDp8Loc],ptrResult);
                ptrResult ++;
                flag[indexDp8] = 1;
                break;
            }
        }
    
        itDp8Loc++;
    }
    delete [] flag;
    return 0;
}

int parseDjpLocations(const Json::Value & valueIn,multi_module_loc ** locationOutArr,int * numOut)
{
    const int numStrDjp = 6;
    std::vector<std::string> strArrDjp(numStrDjp);
    * numOut = valueIn.size();
     
    * locationOutArr = new multi_module_loc[* numOut]; 
    multi_module_loc * ptrResult = * locationOutArr;  
    strArrDjp[0] = "tube_8holes_a";
    strArrDjp[1] = "tube_8holes_b";
    strArrDjp[2] = "reagent";
    strArrDjp[3] = "split";
    strArrDjp[4] = "clean";
    strArrDjp[5] = "mag_shake";



    std::cout<<"size Arr ="<<strArrDjp.size()<<std::endl;
    char * flag = new char[numStrDjp];
    memset(flag,0,numStrDjp);

    Json::Value::Members listNameModule = valueIn.getMemberNames();
    Json::Value::Members::iterator itDjpLoc = listNameModule.begin();
    
    //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
    int indexDjp = 0;   
    while(itDjpLoc!=listNameModule.end())
    {
        indexDjp = 0;
        for(indexDjp=(strArrDjp.size()-1);indexDjp>=0;indexDjp--)
        {
            if(flag[indexDjp])
            {
                continue;
            }
            if((*itDjpLoc).compare(strArrDjp[indexDjp])==0)
            {
                switch(indexDjp)
                {
                    case 0:
                    {
                        ptrResult->locationType = DJ_PURIFY_8HOLES_A;                            
                    }
                    break;
                    case 1:
                    {
                        ptrResult->locationType = DJ_PURIFY_8HOLES_B;                            
                    }
                    break;
                    case 2:
                    {
                        ptrResult->locationType = DJ_PURIFY_REAGENT;                            
                    }
                    break;
                    case 3:
                    {
                        ptrResult->locationType = DJ_PURIFY_SPLIT;                            
                    }
                    break;
                    case 4:
                    {
                        ptrResult->locationType = DJ_PURIFY_CLEANPOOL;                            
                    }
                    break;
                    case 5:
                    {
                        ptrResult->locationType = DJ_PURIFY_MAG_SHAKE;                            
                    }
                    break;

                    default:
                    {
                        std::cout<<*itDjpLoc<<" is shoot"<<",index="<<indexDjp<<std::endl;
                    }
                    break;
                }
                std::cout<<*itDjpLoc<<",set type to "<<ptrResult->locationType<<",index="<<indexDjp<<std::endl;
                get_one_loc_group(valueIn[*itDjpLoc],ptrResult);
                ptrResult ++;
                flag[indexDjp] = 1;
                break;
            }
        }
    
        itDjpLoc++;
    }
    delete [] flag;
    return 0;
}

int parseDjqLocations(const Json::Value & valueIn,multi_module_loc ** locationOutArr,int * numOut)
{
    const int numStrDjq = 6;
    std::vector<std::string> strArrDjq(numStrDjq);
    * numOut = valueIn.size();
     
    * locationOutArr = new multi_module_loc[* numOut]; 
    multi_module_loc * ptrResult = * locationOutArr;  
    strArrDjq[0] = "board_48holes";
    strArrDjq[1] = "trans";
    strArrDjq[2] = "reagent";
    strArrDjq[3] = "barcode";
    strArrDjq[4] = "clean";
    strArrDjq[5] = "board_jaw";


    std::cout<<"size Arr ="<<strArrDjq.size()<<std::endl;
    char * flag = new char[numStrDjq];
    memset(flag,0,numStrDjq);

    Json::Value::Members listNameModule = valueIn.getMemberNames();
    Json::Value::Members::iterator itDjqLoc = listNameModule.begin();
    
    //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
    int indexDjq = 0;   
    while(itDjqLoc!=listNameModule.end())
    {
        indexDjq = 0;
        for(indexDjq=(strArrDjq.size()-1);indexDjq>=0;indexDjq--)
        {
            if(flag[indexDjq])
            {
                continue;
            }
            if((*itDjqLoc).compare(strArrDjq[indexDjq])==0)
            {
                switch(indexDjq)
                {
                    case 0:
                    {
                        ptrResult->locationType = DJ_QPCR_48HOLES;                            
                    }
                    break;
                    case 1:
                    {
                        ptrResult->locationType = DJ_QPCR_TRANS;                            
                    }
                    break;
                    case 2:
                    {
                        ptrResult->locationType = DJ_QPCR_REAGENT;                            
                    }
                    break;
                    case 3:
                    {
                        ptrResult->locationType = DJ_QPCR_BARCODE;                            
                    }
                    break;
                    case 4:
                    {
                        ptrResult->locationType = DJ_QPCR_CLEANPOOL;                            
                    }
                    break;
                    case 5:
                    {
                        ptrResult->locationType = DJ_QPCR_BOARDJAW;                            
                    }
                    break;

                    default:
                    {
                        std::cout<<*itDjqLoc<<" is shoot"<<",index="<<indexDjq<<std::endl;
                    }
                    break;
                }
                std::cout<<*itDjqLoc<<",set type to "<<ptrResult->locationType<<",index="<<indexDjq<<std::endl;
                get_one_loc_group(valueIn[*itDjqLoc],ptrResult);
                ptrResult ++;
                flag[indexDjq] = 1;
                break;
            }
        }
    
        itDjqLoc++;
    }
    delete [] flag;
    return 0;
}


//get multi modules for one move module
int get_multi_modules(const Json::Value & valueIn, multi_module_loc ** locationArrOut, int *numOut, int moduleIdIn)
{
    int numLoc = valueIn.size();

    if(numLoc<0)
    { 
        return -1;
    }

    multi_module_loc * resultArr =  new multi_module_loc[numLoc];

    multi_module_loc * ptrResult = resultArr;


    Json::Value::Members listNameModule = valueIn.getMemberNames();
    Json::Value::Members::iterator itDp1Loc = listNameModule.begin();
    
    
    if(moduleIdIn == MODULE_DP1)
    {
        const int numStrDp1 = 17;
        std::vector<std::string> strArrDp1(numStrDp1);

        strArrDp1[0] = "tip96";
        strArrDp1[1] = "tip_inter_ref";
        strArrDp1[2] = "tube8_hole_a";
        strArrDp1[3] = "tube8_hole_b";
        strArrDp1[4] = "tube8_tube_a";
        strArrDp1[5] = "tube8_tube_b";
        strArrDp1[6] = "tube8_shake_jaw";
        strArrDp1[7] = "tube8_shake_tip";
        strArrDp1[8] = "droptip";

        strArrDp1[9] = "tube8_buff_hole";
        strArrDp1[10] = "tube8_buff_jaw";
        strArrDp1[11] = "droptube8";
        strArrDp1[12] = "openlid_suck";
        strArrDp1[13] = "openlid_jaw";
        strArrDp1[14] = "tube1_jaw";
        strArrDp1[15] = "reagent_jaw";
        strArrDp1[16] = "mag_shake_jaw";


        std::cout<<"size Arr ="<<strArrDp1.size()<<std::endl;
        char * flag = new char[numStrDp1];
        memset(flag,0,numStrDp1);
        
        //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
        int indexDp1 = 0;   
        while(itDp1Loc!=listNameModule.end())
        {
            indexDp1 = 0;
            for(indexDp1=(strArrDp1.size()-1);indexDp1>=0;indexDp1--)
            {
                if(flag[indexDp1])
                {
                    continue;
                }
                if((*itDp1Loc).compare(strArrDp1[indexDp1])==0)
                {
                    switch(indexDp1)
                    {
                        case 0:
                        {
                            //std::cout<<"set type to DP1_TIPS"<<std::endl;
                            ptrResult->locationType = DP1_TIPS;                            
                        }
                        break;
                        case 1:
                        {
                            //std::cout<<"set type to DP1_TIPS"<<std::endl;
                            ptrResult->locationType = DP1_TIPS_INTER_REF;                            
                        }
                        break;
                        case 2:
                        {
                            ptrResult->locationType = DP1_8HOLES_SPIT_A;                            
                        }
                        break;
                        case 3:
                        {
                            ptrResult->locationType = DP1_8HOLES_SPIT_B;                            
                        }
                        break;
                        case 4:
                        {
                            ptrResult->locationType = DP1_8HOLES_JAW_A;                            
                        }
                        break;
                        case 5:
                        {
                            ptrResult->locationType = DP1_8HOLES_JAW_B;                            
                        }
                        break;
                        case 6:
                        {
                            ptrResult->locationType = DP1_TUBE8_SHAKE_JAW;                            
                        }
                        break;
                        case 7:
                        {
                            ptrResult->locationType = DP1_8HOLES_SHAKE_TIP;                            
                        }
                        break;
                        case 8:
                        {
                            ptrResult->locationType = DP1_DROPTIP;                            
                        }
                        break;
                        case 9:
                        {
                            ptrResult->locationType = DP1_8HOLES_SPIT_BUFF;                            
                        }
                        break;                        
                        case 10:
                        {
                            ptrResult->locationType = DP1_8HOLES_BUFF_JAW;                            
                        }
                        break;

                        case 11:
                        {
                            ptrResult->locationType = DP1_DROPTUBE;                            
                        }
                        break;
                        case 12:
                        {
                            ptrResult->locationType = DP1_OPENLID_SUCK;                            
                        }
                        break;
                        case 13:
                        {
                            ptrResult->locationType = DP1_OPENLID_JAW;                            
                        }
                        break;
                        case 14:
                        {
                            ptrResult->locationType = DP1_TUBE_JAW;                            
                        }
                        break;
                        case 15:
                        {
                            ptrResult->locationType = DP1_REAGENT_JAW;                            
                        }
                        break;
                        case 16:
                        {
                            ptrResult->locationType = DP1_MAG_SHAKE_JAW;                            
                        }
                        break;
                        default:
                        {
                            std::cout<<*itDp1Loc<<" is shoot"<<",index="<<indexDp1<<std::endl;
                        }
                        break;
                    }
                    get_one_loc_group(valueIn[*itDp1Loc],ptrResult);
                    ptrResult ++;
                    flag[indexDp1] = 1;
                    break;
                }
            }
        
            itDp1Loc++;
        }
        delete [] flag;   

    }

    *locationArrOut = resultArr;   
    *numOut = numLoc;
    return 0;
}

int parseSealLocations(const Json::Value & valueIn, locs_seal **sealLocsOut)
{
    *sealLocsOut = new locs_seal;

    locs_seal * pstSealLocs = *sealLocsOut;

    Json::Value::Members listNameAxis = valueIn.getMemberNames();
    Json::Value::Members::iterator itSealLoc = listNameAxis.begin();
    
    const int numStrSeal = 4;
    std::vector<std::string> strArrSeal(numStrSeal);
    strArrSeal[0] = "x_wait";
    strArrSeal[1] = "x_work";
    strArrSeal[2] = "z_wait";
    strArrSeal[3] = "z_work";


    std::cout<<"size Arr ="<<strArrSeal.size()<<std::endl;
    char * flag = new char[numStrSeal];
    memset(flag,0,numStrSeal);
    
    //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
    int indexSeal = 0;   
    while(itSealLoc!=listNameAxis.end())
    {
        indexSeal = 0;
        for(indexSeal=(strArrSeal.size()-1);indexSeal>=0;indexSeal--)
        {
            if(flag[indexSeal])
            {
                continue;
            }
            if((*itSealLoc).compare(strArrSeal[indexSeal])==0)
            {
                switch(indexSeal)
                {
                    case 0:
                    {
                        pstSealLocs->x_wait =  valueIn[*itSealLoc].asInt();                           
                    }
                    break;
                    case 1:
                    {
                        pstSealLocs->x_work =  valueIn[*itSealLoc].asInt();                            
                    }
                    break;
                    case 2:
                    {
                        pstSealLocs->z_wait = valueIn[*itSealLoc].asInt();                            
                    }
                    break;
                    case 3:
                    {
                        pstSealLocs->z_work = valueIn[*itSealLoc].asInt();                            
                    }
                    break;                    

                    default:
                    {
                        std::cout<<*itSealLoc<<" is shoot"<<",index="<<indexSeal<<std::endl;
                    }
                    break;
                }

                flag[indexSeal] = 1;
                break;
            }
        }
    
        itSealLoc++;
    }
    delete [] flag;   
    return 0;
}

int parseOpenlidLocations(const Json::Value & valueIn, locs_seal **openlidLocsOut)
{
    *openlidLocsOut = new locs_seal;

    locs_seal * pstOpenlidLocs = *openlidLocsOut;

    Json::Value::Members listNameAxis = valueIn.getMemberNames();
    Json::Value::Members::iterator itOpenlidLoc = listNameAxis.begin();
    
    const int numStrOpenlid = 4;
    std::vector<std::string> strArrOpenlid(numStrOpenlid);
    strArrOpenlid[0] = "x_wait";
    strArrOpenlid[1] = "x_work";
    strArrOpenlid[2] = "z_wait";
    strArrOpenlid[3] = "z_work";


    std::cout<<"size Arr ="<<strArrOpenlid.size()<<std::endl;
    char * flag = new char[numStrOpenlid];
    memset(flag,0,numStrOpenlid);
    
    //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
    int indexOpenlid = 0;   
    while(itOpenlidLoc!=listNameAxis.end())
    {
        indexOpenlid = 0;
        for(indexOpenlid=(numStrOpenlid-1);indexOpenlid>=0;indexOpenlid--)
        {
            if(flag[indexOpenlid])
            {
                continue;
            }
            if((*itOpenlidLoc).compare(strArrOpenlid[indexOpenlid])==0)
            {
                switch(indexOpenlid)
                {
                    case 0:
                    {
                        pstOpenlidLocs->x_wait =  valueIn[*itOpenlidLoc].asInt();                           
                    }
                    break;
                    case 1:
                    {
                        pstOpenlidLocs->x_work =  valueIn[*itOpenlidLoc].asInt();                            
                    }
                    break;
                    case 2:
                    {
                        pstOpenlidLocs->z_wait = valueIn[*itOpenlidLoc].asInt();                            
                    }
                    break;
                    case 3:
                    {
                        pstOpenlidLocs->z_work = valueIn[*itOpenlidLoc].asInt();                            
                    }
                    break;                    

                    default:
                    {
                        std::cout<<*itOpenlidLoc<<" is shoot"<<",index="<<indexOpenlid<<std::endl;
                    }
                    break;
                }

                flag[indexOpenlid] = 1;
                break;
            }
        }
    
        itOpenlidLoc++;
    }
    delete [] flag;   
    return 0;
}

int parseOneQpcrLocation(const Json::Value & valueIn, std::vector<std::string> & strArrIn, locs_qpcr *qpcrLocOut)
{
    int numStrQpcr = strArrIn.size();
    char * flag = new char[numStrQpcr];
    memset(flag,0,numStrQpcr);

    Json::Value::Members listNameAxis = valueIn.getMemberNames();
    Json::Value::Members::iterator itQpcrLoc = listNameAxis.begin();
    
    //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
    int indexQpcr = 0; 

    while(itQpcrLoc!=listNameAxis.end())
    {
        indexQpcr = 0;
        for(indexQpcr=(numStrQpcr - 1);indexQpcr>=0;indexQpcr--)
        {
            if(flag[indexQpcr])
            {
                continue;
            }
            if((*itQpcrLoc).compare(strArrIn[indexQpcr])==0)
            {
                switch(indexQpcr)
                {
                    case 0:
                    {
                        qpcrLocOut->id =  valueIn[*itQpcrLoc].asInt();                           
                    }
                    break;
                    case 1:
                    {
                        qpcrLocOut->bath_x_wait =  valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;
                    case 2:
                    {
                        qpcrLocOut->bath_x_work = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;
                    case 3:
                    {
                        qpcrLocOut->bath_z_wait = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break; 
                    case 4:
                    {
                        qpcrLocOut->bath_z_work = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;
                    case 5:
                    {
                        qpcrLocOut->chanPos[OPTICS_CHANNEL1_CY5] = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;
                    case 6:
                    {
                        qpcrLocOut->chanPos[OPTICS_CHANNEL2_ROX] = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;                    
                    case 7:
                    {
                        qpcrLocOut->chanPos[OPTICS_CHANNEL3_TAMRA] = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;
                    case 8:
                    {
                        qpcrLocOut->chanPos[OPTICS_CHANNEL4_HEX] = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;
                    case 9:
                    {
                        qpcrLocOut->chanPos[OPTICS_CHANNEL5_FAM] = valueIn[*itQpcrLoc].asInt();                            
                    }
                    break;  
                    default:
                    {
                        std::cout<<*itQpcrLoc<<" is shoot"<<",index="<<indexQpcr<<std::endl;
                    }
                    break;
                }

                flag[indexQpcr] = 1;
                break;
            }
        }
    
        itQpcrLoc++;
    }
    delete [] flag; 
    return 0;
}

void print_qpcr_locations(locs_qpcr * qpcrLocArr, int locNum)
{   
    int indexNum = 0;
    locs_qpcr* pstQpcrLoc = qpcrLocArr;
    for(indexNum=0;indexNum<locNum;indexNum++)
    {
        if(pstQpcrLoc==NULL)
        {
            break;
        }
        printf("index=%d,qid=%d,z_work:%d,z_wait:%d,x_work:%d,x_wait:%d\n",
            indexNum,pstQpcrLoc->id,pstQpcrLoc->bath_z_work,pstQpcrLoc->bath_z_wait,
            pstQpcrLoc->bath_x_work,pstQpcrLoc->bath_x_wait);

        printf("CY5:%d,ROX:%d,TAMRA:%d,HEX:%d,FAM:%d\n",
            pstQpcrLoc->chanPos[0],
            pstQpcrLoc->chanPos[1],
            pstQpcrLoc->chanPos[2],
            pstQpcrLoc->chanPos[3],
            pstQpcrLoc->chanPos[4]);

        pstQpcrLoc++;
    }
}

int parseQpcrLocations(const Json::Value & valueIn, locs_qpcr **qpcrLocsOut, int *numOut)
{    
    const int numStrQpcr = 10;
    std::vector<std::string> strArrQpcr(numStrQpcr);
    strArrQpcr[0] = "id";
    strArrQpcr[1] = "x_wait";
    strArrQpcr[2] = "x_work";
    strArrQpcr[3] = "z_wait";
    strArrQpcr[4] = "z_work";
    strArrQpcr[5] = "cy5";
    strArrQpcr[6] = "rox";
    strArrQpcr[7] = "tamra";
    strArrQpcr[8] = "hex";
    strArrQpcr[9] = "fam";


    std::cout<<"size Arr ="<<strArrQpcr.size()<<std::endl;
    char * flag = new char[numStrQpcr];
    memset(flag,0,numStrQpcr);
    
    //std::vector<std::string>::iterator itStrDp1 = strArrDp1.begin();
    int indexQpcr = 0; 
    if(valueIn.isArray())
    {
        int sizeArr = valueIn.size();
        *qpcrLocsOut = new locs_qpcr[sizeArr];
        locs_qpcr * pstOneQpcrConfig = *qpcrLocsOut;

        for(int i= 0;i<sizeArr;i++)
        {
            parseOneQpcrLocation(valueIn[i],strArrQpcr,pstOneQpcrConfig);
            pstOneQpcrConfig ++;
        }
        *numOut = sizeArr;
        
    }
    else if(valueIn.isObject())
    {
        *numOut = 1;
        *qpcrLocsOut = new locs_qpcr;
        parseOneQpcrLocation(valueIn,strArrQpcr,*qpcrLocsOut);
    }  
       
    return 0;
}

void parseOneZLocation(const Json::Value & valueIn,dynamic_z * zlocOut)
{
    if(zlocOut==NULL)
    {
        return;
    }
    int numStr = 2;
    std::vector<std::string> zlocStrArr(numStr);
    zlocStrArr[0] = "tube_mouth";
    zlocStrArr[1] = "z_max";

    int numStrZloc = zlocStrArr.size();
    char * flag = new char[numStrZloc];
    memset(flag,0,numStrZloc);

    Json::Value::Members listNameInJson = valueIn.getMemberNames();
    Json::Value::Members::iterator itZLoc = listNameInJson.begin();
    
    int index = 0;
    while(itZLoc!=listNameInJson.end())
    {
       for(index=(numStr-1);index>=0;index--)
       {
           if(flag[index])
           {
               continue;
           }

           if((*itZLoc).compare(zlocStrArr[index])==0)
           {
               if(index == 0)
               {
                   zlocOut->z_tube_mouth = valueIn[*itZLoc].asInt();
               }
               else if(index==1)
               {
                   zlocOut->z_max = valueIn[*itZLoc].asInt();
               }
               flag[index] = 1;
               break;
           }
       }
       itZLoc++;
    }

    if(flag)
    {
        delete [] flag;
        flag = NULL;
    }
}

void parseZLocations(const Json::Value & valueIn,dynamic_z * locArrOut)
{
    if(locArrOut==NULL)
    {
        return;
    }
    
    std::vector<std::string> zlocStrArr(Z_LOC_MAX_NUM);
    zlocStrArr[0] = "dp1_openlid_jaw";
    zlocStrArr[1] = "dp1_openlid_tip";
    zlocStrArr[2] = "dp1_sample_jaw";
    zlocStrArr[3] = "dp1_tube8_jaw";
    zlocStrArr[4] = "dp1_tube8_buff_jaw";
    zlocStrArr[5] = "dp1_tube8_buff_tip";
    zlocStrArr[6] = "dp1_shake_jaw";
    zlocStrArr[7] = "dp1_get_tip";
    zlocStrArr[8] = "dp1_get_tip_inter_ref";
    zlocStrArr[9] = "dp1_drop_tube"; 
    zlocStrArr[10] = "dp1_reagent_jaw";
    zlocStrArr[11] = "dp1_mag_shake_jaw";       
    zlocStrArr[12] = "dp1_drop_tip";
    zlocStrArr[13] = "dj_purify_reagent";
    zlocStrArr[14] = "dj_purify_tube8";
    zlocStrArr[15] = "dj_purify_mag_shake";
    zlocStrArr[16] = "dj_purify_clean";
    zlocStrArr[17] = "dj_qpcr_reagent";
    zlocStrArr[18] = "dj_qpcr_48holes";

    zlocStrArr[19] = "dj_qpcr_boardjaw";
    zlocStrArr[20] = "dj_qpcr_trans";

    zlocStrArr[21] = "dj_qpcr_clean";
    zlocStrArr[22] = "dp8_get_tip";
    zlocStrArr[23] = "dp8_get_tip_1";
    zlocStrArr[24] = "dp8_drop_tip";
    zlocStrArr[25] = "dp8_drop_tip_1";
	zlocStrArr[26] = "dp8_drop_tip_drop";
    zlocStrArr[27] = "dp8_reagent";
    zlocStrArr[28] = "dp8_tube8";
    zlocStrArr[29] = "dp8_dna_buff";
    zlocStrArr[30] = "dp8_dna_buff_1";
    zlocStrArr[31] = "dp8_suck_pump";

    zlocStrArr[32] = "dp8_trans_jaw";
    zlocStrArr[33] = "dp8_dna_buff_jaw";
    zlocStrArr[34] = "dp8_pcr_buff_jaw";
    zlocStrArr[35] = "dp8_seal_jaw";
    zlocStrArr[36] = "dp8_film_jaw";
    zlocStrArr[37] = "dp8_drop_liquid";
    zlocStrArr[38] = "dp8_pcr_buff_tip8";
    zlocStrArr[39] = "dp8_pcr_buff_1";

    int numStrZloc = zlocStrArr.size();
    char * flag = new char[numStrZloc];
    memset(flag,0,numStrZloc);

    Json::Value::Members listNameInJson = valueIn.getMemberNames();
    Json::Value::Members::iterator itZLoc = listNameInJson.begin();
    dynamic_z * pstResult = locArrOut;
    int index = 0;
    while(itZLoc!=listNameInJson.end())
    {
        pstResult = locArrOut;
        for(index=(numStrZloc-1);index>=0;index--)
        {
            if(flag[index])
            {
                continue;
            }
            if((*itZLoc).compare(zlocStrArr[index])==0)
            {
                pstResult +=  index;
                flag[index] = 1;
                parseOneZLocation(valueIn[*itZLoc],pstResult);
                break;
            }
        }
        itZLoc ++;
    }

    if(flag)
    {
        delete [] flag;
        flag = NULL;
    }

}

void parseOneLogLocation(const Json::Value & valueIn,logistic_loc * logLocOut)
{
    if(logLocOut==NULL)
    {
        return;
    }
    int numStr = 2;
    std::vector<std::string> logLocStrArr(numStr);
    logLocStrArr[0] = "x_y_base";
    logLocStrArr[1] = "z_ch";

    int numStrZloc = logLocStrArr.size();
    char * flag = new char[numStrZloc];
    memset(flag,0,numStrZloc);

    Json::Value::Members listNameInJson = valueIn.getMemberNames();
    Json::Value::Members::iterator itLogLoc = listNameInJson.begin();
    
    int index = 0;
    while(itLogLoc!=listNameInJson.end())
    {
       for(index=(numStr-1);index>=0;index--)
       {
           if(flag[index])
           {
               continue;
           }

           if((*itLogLoc).compare(logLocStrArr[index])==0)
           {
               if(index == 0)
               {
                   logLocOut->x_y_base = valueIn[*itLogLoc].asInt();
               }
               else if(index==1)
               {
                   logLocOut->z_change = valueIn[*itLogLoc].asInt();
               }
               flag[index] = 1;
               break;
           }
       }
       itLogLoc++;
    }
    
    if(flag)
    {
        delete [] flag;
        flag = NULL;
    }
}

void parseLogisticLocs(const Json::Value & valueIn,logistic_loc * locArrOut)
{
    if(locArrOut==NULL)
    {
        return;
    }
    
    std::vector<std::string> loglocStrArr(MAX_NUM_LOG_LOCS);
    loglocStrArr[0] = "pcr_board_wait";
    loglocStrArr[1] = "log1_z_down";
    loglocStrArr[2] = "log1_exchange12";
    loglocStrArr[3] = "log2_exchange12";
    loglocStrArr[4] = "exchange_before";
    loglocStrArr[5] = "exchange_after";
    loglocStrArr[6] = "log2_exchange23";
    loglocStrArr[7] = "up_z";
    loglocStrArr[8] = "qpcr_log";
    loglocStrArr[9] = "log_drop"; 
    loglocStrArr[10] = "qpcr_bath1";
    loglocStrArr[11] = "qpcr_bath2";

    int numStrZloc = loglocStrArr.size();
    char * flag = new char[numStrZloc];
    memset(flag,0,numStrZloc);

    Json::Value::Members listNameInJson = valueIn.getMemberNames();
    Json::Value::Members::iterator itZLoc = listNameInJson.begin();
    logistic_loc * pstResult = locArrOut;
    int index = 0;
    while(itZLoc!=listNameInJson.end())
    {
        pstResult = locArrOut;
        for(index=(numStrZloc-1);index>=0;index--)
        {
            if(flag[index])
            {
                continue;
            }
            if((*itZLoc).compare(loglocStrArr[index])==0)
            {
                pstResult +=  index;
                flag[index] = 1;
                parseOneLogLocation(valueIn[*itZLoc],pstResult);
                break;
            }
        }
        itZLoc ++;
    }

    if(flag)
    {
        delete [] flag;
        flag = NULL;
    }

}

CGpLayout::~CGpLayout()
{
    if(m_dp1_locations)
    {
        delete [] m_dp1_locations;
        m_dp1_locations = NULL;
    }

    if(m_dp8_locations)
    {
        delete [] m_dp8_locations;
        m_dp8_locations = NULL;
    }

    if(m_dj_pu_locations)
    {
        delete [] m_dj_pu_locations;
        m_dj_pu_locations = NULL;
    }
    
    if(m_dj_qpcr_locations)
    {
        delete [] m_dj_qpcr_locations;
        m_dj_qpcr_locations = NULL;
    }

    if(m_pstSealLocs)
    {
        delete m_pstSealLocs;
        m_pstSealLocs = NULL;
    }

    if(m_pstQpcrLocs)
    {
        delete [] m_pstQpcrLocs;
        m_pstQpcrLocs = NULL;
    }

    if(m_pstOpenlidLocs)
    {
        delete [] m_pstOpenlidLocs;
        m_pstOpenlidLocs = NULL;
    }

    if(m_z_locsArr)
    {
        delete [] m_z_locsArr;
        m_z_locsArr = NULL;        
    }
    
    if(m_pstLogLocs)
    {
        delete [] m_pstLogLocs;
        m_pstLogLocs = NULL; 
    }


    std::cout<<"destruct CGpLayout"<<std::endl;
}


void printZlocations(dynamic_z * z_locations)
{
    int i = 0;
    if(z_locations==NULL)
    {
        return;
    }
    dynamic_z * pstZloc = z_locations;
    for(i=0;i<Z_LOC_MAX_NUM;i++)
    {
        printf("i=%d=====\n",i);
        printf("tube_mouth:%d,z_max:%d\n",pstZloc->z_tube_mouth,pstZloc->z_max);
        pstZloc++;
    }
}

void printLoglocations(logistic_loc * log_locations)
{
    int i = 0;
    if(log_locations==NULL)
    {
        return;
    }
    logistic_loc * pstLogLoc = log_locations;
    for(i=0;i<MAX_NUM_LOG_LOCS;i++)
    {
        printf("i=%d=====\n",i);
        printf("x_y_base:%d,z_change:%d\n",pstLogLoc->x_y_base,pstLogLoc->z_change);
        pstLogLoc++;
    }
}



int CGpLayout::load_layout(const char * fileIn)
{

    Json::Reader reader;
    Json::Value root;
 
    //从文件中读取，保证当前文件有test.json文件
    std::ifstream in;//(configFiles_json[fileId], std::ios::binary);

    if(fileIn)
    {
        in.open(fileIn, std::ios::binary);
    }
    else
    {
        in.open(LAYOUT_FILE, std::ios::binary);
    }
    
    if( !in.is_open() )
    {
        std::cout << "Error opening file,"<<fileIn<<std::endl;
        return -1; 
    }
    //std::cout<<"enter in read_param_json"<<std::endl;
//    if(fileIn)
//    {
//       reader.parse(fileIn,root); 
//    }
//    else
//    {
//        in.open(LAYOUT_FILE, std::ios::binary);
//        if( !in.is_open() )  
//        { 
//            std::cout << "Error opening file,"<<LAYOUT_FILE<<std::endl; 
//            return -1; 
//        }
//        reader.parse(in,root); 

//    }

    reader.parse(in,root);
 
    
    if(root.empty())
    {
        std::cout << "Error parse json content\n"; 
        root.clear();
        in.close(); 
        return -2;
    }

    m_pstLogLocs = NULL;
    m_z_locsArr = NULL;
    
    Json::Value::Members listNameModule = root.getMemberNames();
    Json::Value::Members::iterator itRoot = listNameModule.begin();
    while(itRoot!=listNameModule.end())
    {
        std::cout<<"node name:"<<*itRoot<<std::endl;
        if((*itRoot).compare("dp1") == 0)
        {
            get_multi_modules(root["dp1"], &m_dp1_locations, &m_numDp1Locs,MODULE_DP1);
        }
        else if((*itRoot).compare("dp8") == 0)
        {
            //get_multi_modules(root["dp8"], &m_dp8_locations, &m_numDp8Locs,MODULE_DP8);
            parseDp8Locations(root["dp8"],&m_dp8_locations,&m_numDp8Locs);
        }
        else if((*itRoot).compare("seal") == 0)
        {
            parseSealLocations(root["seal"],&m_pstSealLocs);
        }
        else if((*itRoot).compare("dj_purify") == 0)
        {
            //parseSealLocations(root["dj_purify"],&m_pstSealLocs);
            parseDjpLocations(root["dj_purify"],&m_dj_pu_locations,&m_numDjpLocs);
        }
        else if((*itRoot).compare("dj_qpcr") == 0 )
        {
            parseDjqLocations(root["dj_qpcr"],&m_dj_qpcr_locations,&m_numDjqLocs);
        }
        else if( (*itRoot).compare("qpcr") == 0)
        {
            parseQpcrLocations(root["qpcr"],&m_pstQpcrLocs,&m_numQpcr);

            //print_qpcr_locations(m_pstQpcrLocs,m_numQpcr);
        }
        else if( (*itRoot).compare("openlid") == 0)
        {
            parseOpenlidLocations(root["openlid"],&m_pstOpenlidLocs);
        }
        else if((*itRoot).compare("dynamic_z") == 0)
        {
            m_z_locsArr = new dynamic_z[Z_LOC_MAX_NUM];

            parseZLocations(root["dynamic_z"],m_z_locsArr);
        }
        else if((*itRoot).compare("logistic") == 0)
        {
            m_pstLogLocs = new logistic_loc[MAX_NUM_LOG_LOCS];

            parseLogisticLocs(root["logistic"],m_pstLogLocs);
        }

        // else if()
        // {

        // }
        itRoot++;
    }  

    root.clear();
    in.close();


    // std::cout<<"num dp1 locs:"<<m_numDp1Locs<<std::endl;
    // multi_module_loc * ptrModuleLoc = m_dp1_locations;
    // for(int i=0;i<m_numDp1Locs;i++)
    // {
    //     std::cout<<"i="<<i<<"------------"<<std::endl;
    //     print_multi_module_loc(ptrModuleLoc);
    //     ptrModuleLoc++;
    // }
    
    // std::cout<<"seal locs:"<<std::endl;
    // std::cout<<"x_wait:"<<m_pstSealLocs->x_wait<<",x_work:"<<m_pstSealLocs->x_work<<std::endl;
    // std::cout<<"z_wait:"<<m_pstSealLocs->z_wait<<",z_work:"<<m_pstSealLocs->z_work<<std::endl;

    // std::cout<<"openlid locs:"<<std::endl;
    // std::cout<<"x_wait:"<<m_pstOpenlidLocs->x_wait<<",x_work:"<<m_pstOpenlidLocs->x_work<<std::endl;
    // std::cout<<"z_wait:"<<m_pstOpenlidLocs->z_wait<<",z_work:"<<m_pstOpenlidLocs->z_work<<std::endl;

    printLoglocations(m_pstLogLocs);
    
    return 0;
}

void print_multi_module_loc(multi_module_loc * locToPrint)
{
    std::cout<<"mudule locationType:"<<locToPrint->locationType<<std::endl<<std::endl;

    std::cout<<"mudule num x :"<<locToPrint->max_num_module_x<<std::endl;
    std::cout<<"mudule num y :"<<locToPrint->max_num_module_y<<std::endl;

    std::cout<<"mudule ex delta x :"<<locToPrint->delta_module_x<<std::endl;
    std::cout<<"mudule ex delta y :"<<locToPrint->delta_module_y<<std::endl;

    
    std::cout<<"in mudule init x :"<<locToPrint->firstModuleLoc.init_x<<std::endl;
    std::cout<<"in mudule init y :"<<locToPrint->firstModuleLoc.init_y<<std::endl;

    std::cout<<"in mudule delta x :"<<locToPrint->firstModuleLoc.delta_x<<std::endl;
    std::cout<<"in mudule delta y :"<<locToPrint->firstModuleLoc.delta_y<<std::endl;

    std::cout<<"in mudule num x :"<<locToPrint->firstModuleLoc.max_num_x<<std::endl;
    std::cout<<"in mudule num y :"<<locToPrint->firstModuleLoc.max_num_y<<std::endl;


}

int CGpLayout::convertToAxisLoc(const location_layout * pstLayoutIn,location_axis * pstLocAxisOut)
{
    multi_module_loc * ptrMultiModuleLoc = NULL;
    int indexLoc = 0;
    int mudule_type = get_module_type(pstLayoutIn->location_type);
    int maxNumIndex = 0;
    if(mudule_type == MODULE_DP1)
    {
        ptrMultiModuleLoc = m_dp1_locations;
        maxNumIndex = m_numDp1Locs;     
        
    }
    else if(mudule_type == MODULE_DJ_PURIFY)
    {
        ptrMultiModuleLoc = m_dj_pu_locations;
        maxNumIndex = m_numDjpLocs;
    }
    else if(mudule_type == MODULE_DJ_QPCR)
    {
        ptrMultiModuleLoc = m_dj_qpcr_locations;
        maxNumIndex = m_numDjqLocs;
    }
    else if(mudule_type == MODULE_SEAL)
    {
        if(pstLayoutIn->location_type == SEAL_X_WAIT)
        {
            pstLocAxisOut->x = m_pstSealLocs->x_wait;
        }
        else if(pstLayoutIn->location_type == SEAL_X_WORK)
        {
            pstLocAxisOut->x = m_pstSealLocs->x_work;
        }
        else if(pstLayoutIn->location_type == SEAL_Z_WORK)
        {
            pstLocAxisOut->z = m_pstSealLocs->z_work;
        }
        else if(pstLayoutIn->location_type == SEAL_Z_WAIT)
        {
            pstLocAxisOut->z = m_pstSealLocs->z_wait;
        }
        else 
        {
            return 2;
        }

        return 0;
    }
    else if(mudule_type == MODULE_DP8)
    {
        ptrMultiModuleLoc = m_dp8_locations;
        maxNumIndex = m_numDp8Locs;        
    }
    else
    {
        return 6;
    }
    

    indexLoc = 0;
    while(ptrMultiModuleLoc->locationType != pstLayoutIn->location_type)
    {
        ptrMultiModuleLoc++;
        indexLoc++;

        if(indexLoc==maxNumIndex)
        {
            ptrMultiModuleLoc = NULL;
            break; 
        }
    }

    if(ptrMultiModuleLoc==NULL)
    {
        return 1;
    }

    int validRet = isParamValid(pstLayoutIn,ptrMultiModuleLoc);

    if(!validRet)
    {
        return 4;
    }
    //for shake, need process specially
    if(pstLayoutIn->location_type == DP1_TUBE8_SHAKE_JAW || 
       DP8_TUBE8_SHAKE == pstLayoutIn->location_type)
    {
        if(pstLayoutIn->indexX == 0 && pstLayoutIn->indexY == 0)
        {
            return 5;
        }
    }

    pstLocAxisOut->x = pstLayoutIn->column_x * ptrMultiModuleLoc->firstModuleLoc.delta_x + ptrMultiModuleLoc->firstModuleLoc.init_x;
    pstLocAxisOut->x += pstLayoutIn->indexX * ptrMultiModuleLoc->delta_module_x;


    pstLocAxisOut->y = pstLayoutIn->row_y * ptrMultiModuleLoc->firstModuleLoc.delta_y + ptrMultiModuleLoc->firstModuleLoc.init_y;
    pstLocAxisOut->y += pstLayoutIn->indexY*ptrMultiModuleLoc->delta_module_y;
    
    return 0;
}

locs_seal * CGpLayout::getOpenlidPos(void)
{
    return m_pstOpenlidLocs;
}


locs_seal * CGpLayout::getSealPos(void)
{
    return m_pstSealLocs;    
}


int CGpLayout::get_module_type(int locTypeIn)
{
    if(locTypeIn>=DP1_TYPE_START && locTypeIn <= DP1_TYPE_END)
    {
        return MODULE_DP1;
    }
    else if(locTypeIn>=DP8_TYPE_START && locTypeIn <= DP8_TYPE_END)
    {
        return MODULE_DP8;
    }
    else if(locTypeIn>=SEAL_TYPE_START && locTypeIn<=SEAL_TYPE_END)
    {
        return MODULE_SEAL;
    }
    else if(locTypeIn>=OPENLID_TYPE_START && locTypeIn<= OPENLID_TYPE_END)
    {
        return MODULE_OPENLID;
    }
    else if(locTypeIn>=DJ_PURIFY_START && locTypeIn<= DJ_PURIFY_END)
    {
        return MODULE_DJ_PURIFY;
    }
    else if(locTypeIn>=DJ_QPCR_START && locTypeIn<= DJ_QPCR_END)
    {
        return MODULE_DJ_QPCR;
    }
    else if(locTypeIn>=QPCR1_TYPE_START && locTypeIn<= QPCR1_TYPE_END)
    {
        return MODULE_QPCR1;
    }
    else if(locTypeIn>=QPCR2_TYPE_START && locTypeIn<= QPCR2_TYPE_END)
    {
        return MODULE_QPCR2;
    }
    else 
    {
        return -1;
    }
    
}


int CGpLayout::isParamValid(const location_layout * pstLayoutIn, const multi_module_loc * ptrLocModules)
{
    int validRet = 0;
    if(pstLayoutIn->indexX < ptrLocModules->max_num_module_x
    && pstLayoutIn->indexX >= 0 
    && pstLayoutIn->indexY < ptrLocModules->max_num_module_y
    && pstLayoutIn->indexY >= 0) 
    {
        if(pstLayoutIn->row_y < ptrLocModules->firstModuleLoc.max_num_y
          && pstLayoutIn->row_y >= 0
          && pstLayoutIn->column_x < ptrLocModules->firstModuleLoc.max_num_x
          && pstLayoutIn->column_x >= 0)
          {
              validRet = 1;
          }

    }

    return validRet;
}

int CGpLayout::getZloc(int locTypeIn,dynamic_z * zlocOut)
{
    if(locTypeIn<0 || locTypeIn > QPCR2_TYPE_END)
    {
        return 0;
    }

    if(zlocOut==NULL)
    {
        return 0;
    }

    int zlocTypeIn = convertToZLocType(locTypeIn);
    
    printf("z loc type:%d\n",zlocTypeIn);
    if(zlocTypeIn<0)
    {
        return 0;
    }

    memcpy(zlocOut,m_z_locsArr + zlocTypeIn,sizeof(dynamic_z));
    return 1;
}


int CGpLayout::getZloc_withoutConvert(int locTypeIn,dynamic_z * zlocOut)
{
    if(locTypeIn<0 || locTypeIn > Z_LOC_MAX_NUM)
    {
        return 0;
    }

    if(zlocOut==NULL)
    {
        return 0;
    }

    
    printf("z loc type:%d\n",locTypeIn);


    memcpy(zlocOut,m_z_locsArr + locTypeIn,sizeof(dynamic_z));
    return 1;
}


int CGpLayout::getQpcrOpticalPos(int QpcrIdIn,int channelId)
{
    locs_qpcr * pstQpcrLoc = m_pstQpcrLocs;
    
    int posRet = -1;

    int maxNumTemp = m_numQpcr;
    int i = 0;
    for(i=0;i<maxNumTemp;i++)
    {
        if((pstQpcrLoc->id - 1) == QpcrIdIn)
        {
            if(channelId >= 0  && channelId < OPTICS_CHANNEL_MAX)
            {
               posRet = pstQpcrLoc->chanPos[channelId]; 
            }
        }
        pstQpcrLoc ++ ;
    }

    return posRet;


}

int CGpLayout::getLogisticLoc(int logLocTypeIn, logistic_loc * logLocOut)
{
    int ret = -1;

    if(logLocTypeIn < LOG_START_PURIFY || logLocTypeIn > LOG_STOP_QPCR)
    {
        return ret;
    }

    if(logLocOut == NULL)
    {
        return ret;
    }

    memcpy(logLocOut,m_pstLogLocs+logLocTypeIn-LOG_START_PURIFY,sizeof(logistic_loc));
    ret = 0;
    return ret;
}

locs_qpcr * CGpLayout::getQpcrPos(int QpcrIdIn)
{
    locs_qpcr * pstPosRet = NULL;
    locs_qpcr * pstQpcrLoc = m_pstQpcrLocs;
    
    int posRet = -1;

    int maxNumTemp = m_numQpcr;
    int i = 0;
    for(i=0;i<maxNumTemp;i++)
    {
        if((pstQpcrLoc->id - 1) == QpcrIdIn)
        {
            pstPosRet = pstQpcrLoc;
            break;
        }
        pstQpcrLoc ++ ;
    }

    return pstPosRet;    
}

//    DJ_QPCR_48HOLES,
//    DJ_QPCR_START = DJ_QPCR_48HOLES,
//    DJ_QPCR_REAGENT,
//    DJ_QPCR_BARCODE,
//    DJ_QPCR_CLEANPOOL,
//    DJ_QPCR_TRANS,
//    DJ_QPCR_END = DJ_QPCR_TRANS,

//    DJ_PURIFY_8HOLES_A,
//    DJ_PURIFY_START = DJ_PURIFY_8HOLES_A,
//    DJ_PURIFY_8HOLES_B,
//    DJ_PURIFY_MAGNET,
//    DJ_PURIFY_REAGENT,
//    DJ_PURIFY_BARCODE,
//    DJ_PURIFY_CLEANPOOL,
//    DJ_PURIFY_END = DJ_PURIFY_CLEANPOOL,

//    DP1_TUBE_JAW,
//    DP1_TYPE_START = DP1_TUBE_JAW,
//    DP1_TIPS,
//    DP1_TIPS_INTER_REF,
//    DP1_8HOLES_JAW_A,
//    DP1_8HOLES_JAW_B,
//    DP1_8HOLES_SPIT_A,
//    DP1_8HOLES_SPIT_B,
//    DP1_8HOLES_SHAKE,
//    DP1_8HOLES_SPIT_BUFF,
//    DP1_8HOLES_BUFF_JAW,
//    DP1_DROPTIP,
//    DP1_DROPTUBE,
//    DP1_OPENLID_JAW,
//    DP1_OPENLID_SUCK,
//    DP1_TYPE_END = DP1_OPENLID_SUCK,


//    DP8_PCR_TRANS,
//    DP8_TYPE_START = DP8_PCR_TRANS,
//    DP8_PCR_BUFF_TIP,
//    DP8_PCR_BUFF_JAW,
//    DP8_DNA_BUFF_JAW,
//    DP8_DNA_BUFF_TIP,
//    DP8_TIP48_TRANS,
//    DP8_TIP48_PURIFY,
//    DP8_TIP48_BUFF,
//    DP8_REAGENT,
//    DP8_TUBE8_SHAKE,
//    DP8_DROPTIP,
//    DP8_FILM_SRC,
//    DP8_TYPE_END = DP8_FILM_SRC,
int convertToZLocType(int locTypeIn)
{
    int ret = -1;
    switch(locTypeIn)
    {
        case DJ_QPCR_48HOLES:
        {
            ret = Z_LOC_DJQ_48HOLES;
        }
        break;
        case DJ_QPCR_REAGENT:
        {
            ret = Z_LOC_DJQ_REAGENT;
        }
        break;
        case DJ_QPCR_CLEANPOOL:
        {
            ret = Z_LOC_DJQ_CLEAN;
        }
        break;
        case DJ_QPCR_BOARDJAW:
        {
            ret = Z_LOC_DJQ_BOARDJAW;
        }
        break;  
        case DJ_QPCR_TRANS:
        {
            ret = Z_LOC_DJQ_TRANSJAW;
        }
        break;               
        case DJ_PURIFY_8HOLES_A:
        {
            ret = Z_LOC_DJP_TUBE8;
        }
        break;
        case DJ_PURIFY_8HOLES_B:
        {
            ret = Z_LOC_DJP_TUBE8;
        }
        break;
        case DJ_PURIFY_SPLIT:
        {
            ret = Z_LOC_DJP_REAGENT;
        }
        break;
        case DJ_PURIFY_REAGENT:
        {
            ret = Z_LOC_DJP_REAGENT;
        }
        break;
        case DJ_PURIFY_MAG_SHAKE:
        {
            ret = Z_LOC_DJP_MAG_SHAKE;
        }
        break;
        case DJ_PURIFY_CLEANPOOL:
        {
            ret = Z_LOC_DJP_CLEAN;
        }
        break;
        case DP1_TUBE_JAW:
        {
            ret = Z_LOC_DP1_SAMPLE_JAW;
        }
        break;
        case DP1_TIPS:
        {
            ret = Z_LOC_DP1_GET_TIP;
        }
        break;
        case DP1_TIPS_INTER_REF:
        {
            ret = Z_LOC_DP1_GET_TIP_INTER_REF;
        }
        break;
        case DP1_8HOLES_JAW_A:
        case DP1_8HOLES_JAW_B:
        {
            ret = Z_LOC_DP1_TUBE8_JAW;
        }
        break;
        case DP1_8HOLES_SPIT_A:
        case DP1_8HOLES_SPIT_B:
        case DP1_8HOLES_SPIT_BUFF:
        {
            ret = Z_LOC_DP1_TUBE8_BUFF_TIP;
        }
        break; 
        case DP1_TUBE8_SHAKE_JAW://
        {
            ret = Z_LOC_DP1_TUBE8_SHAKE_JAW;
        }
        break; 
        case DP1_8HOLES_BUFF_JAW:
        {
            ret = Z_LOC_DP1_TUBE8_BUFF_JAW;
        }
        break;        
        case DP1_8HOLES_SHAKE_TIP:
        {
            ret = Z_LOC_DP1_TUBE8_BUFF_TIP;
        }
        break;        
        case DP1_DROPTIP:
        {
            ret = Z_LOC_DP1_DROP_TIP;
        }
        break;
        case DP1_DROPTUBE:
        {
            ret = Z_LOC_DP1_DROP_TUBE8_JAW;
        }
        break;
        case DP1_OPENLID_JAW:
        {
            ret = Z_LOC_DP1_OPENLID_JAW;
        }
        break;
        case DP1_MAG_SHAKE_JAW:
        {
            ret = Z_LOC_DP1_MAG_SHAKE_JAW;
        }
        break;
        case DP1_REAGENT_JAW:
        {
            ret = Z_LOC_DP1_REAGENT_JAW;
        }
        break;
		
        case DP1_OPENLID_SUCK:
        {
            ret = Z_LOC_DP1_OPENLID_TIP;
        }
        break;
        case DP8_PCR_TRANS:
        {
            ret = Z_LOC_DP8_TRANS_JAW;
        }
        break;
        case DP8_PCR_BUFF_TIP:
        {
            ret = Z_LOC_DP8_PCR_BUFF;
        }
        break;
        case DP8_PCR_BUFF_TIP_1:
        {
            ret = Z_LOC_DP8_PCR_BUFF_1;
        }
        break;
        case DP8_PCR_BUFF_JAW:
        {
            ret = Z_LOC_DP8_PCR_BUFF_JAW;
        }
        break;
        case DP8_DNA_BUFF_JAW:
        {
            ret = Z_LOC_DP8_DNA_BUFF_JAW;
        }
        break;
        case DP8_DNA_BUFF_TIP:
        {
            ret = Z_LOC_DP8_DNA_BUFF;
        }
        break;
        case DP8_DNA_BUFF_TIP_1:
        {
            ret = Z_LOC_DP8_DNA_BUFF_1;
        }
        break;
        case DP8_TIP48_TRANS:
        {
            ret = Z_LOC_DP8_GET_TIP;
        }
        break;
        case DP8_TIP48_PURIFY_A:
        case DP8_TIP48_PURIFY_B:
        {
            ret = Z_LOC_DP8_GET_TIP;
        }
        break;
        case DP8_TIP48_BUFF:
        {
            ret = Z_LOC_DP8_GET_TIP;
        }
        break;
        case DP8_REAGENT:
        {
            ret = Z_LOC_DP8_REAGENT;
        }
        break;
        case DP8_TUBE8_SHAKE:
        {
            ret = Z_LOC_DP8_TUBE8;
        }
        break;
        case DP8_DROPTIP:
        {
            ret = Z_LOC_DP8_DROP_TIP;
        }
        break;        
        case DP8_FILM_SRC:
        {
            ret = Z_LOC_DP8_FILM_JAW;
        }
        break; 
        case DP8_HEAT_SEAL:
        {
            ret = Z_LOC_DP8_SEAL_JAW;
        }               
        default:
        break;
    }

    return ret;
}